package net.zhengxinyun.performance.controller.indicators;


import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.*;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.service.departClass.DepartClassService;
import net.zhengxinyun.performance.service.departClass.DepartHeadService;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.indicators.*;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 县指标控制层
 * @Auther: hyp
 * @Date: 2018/10/26 16:11
 */
@RestController
@RequestMapping("/countryUnitIndicators")
public class CountryUnitIndicatorsController {

    private CountryUnitIndicatorsService countryUnitIndicatorsService;

    private TokenService tokenService;

    private CountryUnitScoreService countryUnitScoreService;

    private UserService userService;

    private LoginService loginService;

    @Autowired
    private DepartClassService departClassService;

    @Autowired
    private TownScoreService townScoreService;

    @Autowired
    private DepartHeadService departHeadService;

    @Autowired
    public CountryUnitIndicatorsController(CountryUnitIndicatorsService countryUnitIndicatorsService, TokenService tokenService,  UserService userService, LoginService loginService,CountryUnitScoreService countryUnitScoreService) {
        this.countryUnitIndicatorsService = countryUnitIndicatorsService;
        this.countryUnitScoreService = countryUnitScoreService;
        this.tokenService = tokenService;
        this.userService = userService;
        this.loginService = loginService;
    }

    /**
     * 指标管理删除
     * @param param
     * @return
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody CountryUnitIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str =countryUnitIndicatorsService.delete(param.countryUnitIndicatorsEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }else {
                // 同步删除单位分类表头
                departHeadService.deleteByIndexId(param.countryUnitIndicatorsEntity.getId());

                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 指标管理修改
     * @param param
     * @return
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody CountryUnitIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            //根据传来的上级指标名设置上级指标id
//            countryUnitIndicatorsService.setIdByName(param.countryUnitIndicatorsEntity.getSuperiorIndexId(),param.countryUnitIndicatorsEntity.getDateTime(),param.countryUnitIndicatorsEntity);
            int i = countryUnitIndicatorsService.update(param.countryUnitIndicatorsEntity);
            if (i == 1){
                //同步更新到单位分类表头。
                DepartClassHead head = new DepartClassHead(param.countryUnitIndicatorsEntity.getIndexName(),
                        param.countryUnitIndicatorsEntity.getDateTime(),
                        param.countryUnitIndicatorsEntity.getId());

                departHeadService.update(head);
                return ResultUtils.getSuccessResults();
            }else {
                return ResultUtils.getResults(Result.Status.ERROR,"更新失败，请联系管理员");
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 指标管理添加
     * @param param
     * @return
     */
    @PostMapping("/insert")
    public Result<String> insert(@RequestBody CountryUnitIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            CountryUnitIndicatorsFilter filter = new CountryUnitIndicatorsFilter();

            if (param.countryUnitIndicatorsEntity!=null) {
                CountryUnitIndicatorsEntity countryUnitIndicatorsEntity = param.countryUnitIndicatorsEntity;
                //根据传来的上级指标名设置上级指标id
//                countryUnitIndicatorsService.setIdByName(countryUnitIndicatorsEntity.getSuperiorIndexId(),countryUnitIndicatorsEntity.getDateTime(),countryUnitIndicatorsEntity);
                countryUnitIndicatorsEntity.setId(null);
                int i = countryUnitIndicatorsService.insert(countryUnitIndicatorsEntity);
                if (i != 0) {
                    //同步插入到单位分类表
                    DepartClassHead head  = new DepartClassHead(param.countryUnitIndicatorsEntity.getIndexName(),
                            param.countryUnitIndicatorsEntity.getDateTime(),param.countryUnitIndicatorsEntity.getId());
                    departHeadService.insertSingle(head);
                    return ResultUtils.getSuccessResults();
                } else {
                    return ResultUtils.getResults(Result.Status.ERROR, "添加失败，请联系管理员");
                }
            }else if (param.list != null){
                for (CountryUnitIndicatorsEntity countryUnitIndicatorsEntity:param.list){
                    filter.setIndexName(countryUnitIndicatorsEntity.getIndexName());
                    filter.setDateTime(countryUnitIndicatorsEntity.getDateTime());
//                    filter.setIndexType(countryUnitIndicatorsEntity.getIndexType());
                    if (countryUnitIndicatorsService.query(filter,userEntity).size()!=0){
                        continue;
                    }
                    if (param.dateTime != null){
                        countryUnitIndicatorsEntity.setDateTime(param.dateTime);
                    }
                   /* if (StringUtils.isNotBlank(countryUnitIndicatorsEntity.getSuperiorIndexId())){
                        //根据传来的上级指标名设置上级指标id
                       countryUnitIndicatorsService.setIdByName(countryUnitIndicatorsEntity.getSuperiorIndexId(),countryUnitIndicatorsEntity.getDateTime(),countryUnitIndicatorsEntity);
                    }*/
                    int i = countryUnitIndicatorsService.insert(countryUnitIndicatorsEntity);
                    if (i == 0) {
                        return ResultUtils.getResults(Result.Status.ERROR, "添加失败，请联系管理员");
                    }
                }
                return ResultUtils.getSuccessResults();
            }
            return ResultUtils.getResults(Result.Status.ERROR);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 指标管理查询
     * @param param
     * @return
     */
    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody CountryUnitIndicatorsParam param){
            if (tokenService.compareToken(param.tokenEntity.getValue())){
                if (param.countryUnitIndicatorsFilter.getPageSize() == 0){
                    param.countryUnitIndicatorsFilter.setPageSize(10);
                }
                HashMap<String,Object> map = new HashMap<>();
                CountryUnitIndicatorsFilter countryUnitIndicatorsFilter = new CountryUnitIndicatorsFilter();
              /*  countryUnitIndicatorsFilter.setIndexType("一级指标");
                List<CountryUnitIndicatorsEntity> firstIndexList = countryUnitIndicatorsService.query(countryUnitIndicatorsFilter);
                map.put("firstIndex",firstIndexList);//给一级指标集合*/

                UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
               /*  不需要按照牵头单位来县直， 按照勾选的指标去筛选
               if (!"admin".equals(userEntity.getUserPermissions())) {
                    param.countryUnitIndicatorsFilter.setLeadUnit(userEntity.getDepartcode());
                }*/

                List<CountryUnitIndicatorsEntity>  list =  countryUnitIndicatorsService.query(param.countryUnitIndicatorsFilter,userEntity);
                if (list.size() == 0){
                    param.countryUnitIndicatorsFilter.setPageNumber(param.countryUnitIndicatorsFilter.getPageNumber()-1);
                    list = countryUnitIndicatorsService.query(param.countryUnitIndicatorsFilter,userEntity);
                }
//                Double pageTotal = Util.intGetTotalPage(countryUnitIndicatorsService.queryForCount(param.countryUnitIndicatorsFilter),param.countryUnitIndicatorsFilter.getPageSize());
                Integer pageTotal = countryUnitIndicatorsService.queryForCount(param.countryUnitIndicatorsFilter);



                map.put("pageTotal",pageTotal);
                Map<String,String > maps = townScoreService.getAllDepartmen();
                for(CountryUnitIndicatorsEntity item: list)
                {
                    item.setLeadUnitName(maps.get(item.getLeadUnit()));

                }


                map.put("list",list);//根据条件查询集合

                DepartClassFilter filter  = new DepartClassFilter();
                // HashMap<String,Object> classmap = new HashMap<>();
                List<DepartClassFilter> classlists=departClassService.query(filter);
                map.put("departclssList",classlists);


                return ResultUtils.getSuccessResults(map);
            }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }





}

class CountryUnitIndicatorsParam{
    public CountryUnitIndicatorsFilter countryUnitIndicatorsFilter;
    public CountryUnitIndicatorsEntity countryUnitIndicatorsEntity;
    public CountryUnitScoreEntity countryUnitScoreEntity;
    public CountryUnitScoreFilter countryUnitScoreFilter;
    public TokenEntity tokenEntity;
    public List<CountryUnitIndicatorsEntity> list;
    public String dateTime;
}
